<template>
  <div class="app-main-container">
    <QueryForm
      v-model="filters"
      :query-form-config="queryFormConfig"
      @handleQuery="getList(1)"
      @clear="clearFilters"
    />
    <el-table
      ref="table"
      v-loading="listLoading"
      :data="list"
      border
      fit
      :header-row-class-name="'table-header'"
      class="doumee-element-table"
    >
      <el-table-column align="center" label="反馈标题" prop="title" min-width="150" show-overflow-tooltip />
      <el-table-column align="center" label="反馈内容" prop="content" min-width="200" show-overflow-tooltip />
      <el-table-column align="center" label="反馈人" prop="memberInfo" min-width="240" show-overflow-tooltip />
      <el-table-column align="center" label="创建时间" prop="createTime" min-width="160" show-overflow-tooltip />
      <el-table-column align="center" label="处理人" prop="userName" min-width="120" show-overflow-tooltip />
      <el-table-column align="center" label="处理时间" prop="editTime" min-width="160" show-overflow-tooltip />
      <el-table-column width="1" />
      <el-table-column align="center" label="状态" width="120" fixed="right">
        <template slot-scope="scope">
          {{
            statusMethod(scope.row.status)
          }}
        </template>
      </el-table-column>
      <el-table-column label="操作" width="200" align="center" fixed="right">
        <template slot-scope="scope">
          <div v-if="scope.row.status==='0'" class="flex-cc">
            <el-button
              v-preventReClick
              type="text"
              @click="toFeed(scope.row)"
            >回复</el-button>
            <el-button
              v-preventReClick
              type="text"
              style="color: red"
              @click="toDelete(scope.row)"
            >忽略</el-button>
          </div>
        </template>
      </el-table-column>
    </el-table>
    <!-- <div class="flex-cc doumee-pagination">
      <el-pagination
        background
        layout="prev, pager, next"
        :total="total"
        :current-page.sync="pagination.page"
        :hide-on-single-page="true"
        @current-change="getList"
      />
    </div> -->
    <Pagination
      :total="total"
      :page="pagination.page"
      @current-change="getList"
      @SizeChange="handleSizeChange"
    />
    <!-- 新增 编辑 消息 -->
    <el-dialog
      :close-on-click-modal="modalDialog"
      :lose-on-press-escape="modalDialog"
      title="处理意见反馈"
      width="500px"
      :visible.sync="showDialog"
      @closed="cancelDialog"
    >
      <el-form
        ref="ruleForm"
        :model="formFeed"
        label-position="top"
      >
        <div class="feedBox">
          <div class="h2">{{ feedCnt.title }}</div>
          <div>{{ feedCnt.content }}</div>
        </div>
        <el-form-item label="官方回复：" prop="replyContent">
          <el-input v-model="formFeed.replyContent" type="textarea" maxlength="300" />
        </el-form-item>
      </el-form>
      <span slot="footer">
        <el-button @click="cancelDialog">取消</el-button>
        <el-button v-preventReClick type="primary" @click="submit">提交</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { feedBackList, feedBackHandle } from '@/api'
import { parseTime } from '@/utils'
import Pagination from '@/components/paginations'
import { queryFormConfig } from './config'
export default {
  name: 'NoticeFeedback',
  components: {
    Pagination
  },
  data() {
    return {
      queryFormConfig,
      modalDialog: this.$store.state.app.dialog,
      list: [],
      pagination: {
        page: 1,
        rows: 10,
        firstQueryTime: ''
      },
      total: 0,
      filters: {},
      statusList: [{
        label: '待处理',
        value: '0'
      }, {
        label: '已处理',
        value: '1'
      }, {
        label: '已忽略',
        value: '2'
      }],
      showDialog: false,
      formFeed: {},
      feedCnt: {},
      rules: {
        replyContent: [
          { required: true, message: '请输入内容', trigger: 'blur' }
          // { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.getList(1)
  },
  mounted() {
    this.$nextTick(() => {
      this.$refs.table.doLayout()
    })
  },
  activated() {
    this.$nextTick(() => {
      this.$refs.table.doLayout()
    })
  },
  methods: {
    handleSizeChange(value) {
      this.pagination.rows = value
      this.getList(1)
    },
    getList(page) {
      const { pagination, filters: param } = this
      if (page) {
        pagination.page = page
        this.pagination.page = page
      }
      if (pagination.page === 1) {
        pagination.firstQueryTime = parseTime(new Date())
      }
      this.listLoading = true
      feedBackList({ pagination, param }).then(res => {
        this.listLoading = false
        if (res.errorCode === '000000') {
          this.list = res.recordList
          this.total = res.totalCount
        }
      }).catch(() => {
        this.listLoading = false
      })
    },
    statusMethod(payM) {
      const temp = this.statusList.find((item) => item.value === payM)
      return temp ? temp.label : '-'
    },
    // 清空筛选项
    clearFilters() {
      this.filters = {}
      this.getList(1)
    },
    // 新增/编辑消息
    toFeed(cnt) {
      this.showDialog = true
      this.formFeed = {}
      this.feedCnt = cnt
    },
    // 取消新增/编辑
    cancelDialog() {
      this.showDialog = false
      this.formFeed = {}
    },
    // 提交
    submit() {
      const param = { ...this.formFeed, id: this.feedCnt.id, status: '1' }
      if (!param.replyContent) {
        return this.$message({
          message: '请输入回复内容',
          type: 'info'
        })
      }
      feedBackHandle({ param }).then(res => {
        if (res.errorCode === '000000') {
          this.getList(1)
          this.showDialog = false
        }
      })
    },
    // 忽略
    toDelete(row) {
      const that = this
      this.$confirm('确定忽略这个意见反馈吗？忽略后不再支持回复', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        feedBackHandle({ param: { id: row.id, status: '2' }}).then(res => {
          if (res.errorCode === '000000') {
            that.getList(1)
          }
        })
      })
        .catch(() => {
          // this.$message.info('已取消')
        })
    }
  }
}
</script>
<style scoped lang="scss">
.feedBox {
  padding: 20px;
  background-color: rgb(242,242,242);
  margin-bottom: 20px;
  border-radius: 5px;
  .h2 {
    font-weight: bolder;
    margin-bottom: 20px;
    color: #333;
  }
}
</style>
